/*
 * File: Lib_Balance.c
 *
 * Code generated for Simulink model 'FLTM'.
 *
 * Model version                  : 3.153
 * Simulink Coder version         : 9.4 (R2020b) 29-Jul-2020
 * C/C++ source code generated on : Mon Aug  2 13:13:30 2021
 */

#include "includes.h"


uint8_T EEPROM_vWriteBytes(uint16_T datalen,uint32_T *writedata)
{
	uint8_T save_data[5];
	uint8_T read_data[5];
	uint8_T ret_sts = 0;;
	static uint8_T  res=0U;
	static uint16_T data_num=0;
	static uint8_T readrtcflg = 0;

	if(vewm_enum_SleepSt==1)
	{
		if(readrtcflg == 0)
		{
			TASK_ReadRTC();
			readrtcflg = 1;
		}

		for(res=0U;res<3U;res++)
		{
			if(data_num < datalen)
			{
				save_data[0] = (uint8_T)(writedata[data_num]>>24);
				save_data[1] = (uint8_T)(writedata[data_num]>>16);
				save_data[2] = (uint8_T)(writedata[data_num]>>8);
				save_data[3] = (uint8_T)(writedata[data_num]>>0);
				save_data[4] = save_data[0] + save_data[1] + save_data[2] + save_data[3];

				OS_ENTER_CRITICAL();
				exEEP_Write_Bytes((FRAM_NVM_ADDR+data_num*5U), save_data, 5);
				OS_EXIT_CRITICAL();
				vTaskDelay(6);
				OS_ENTER_CRITICAL();
				exEEP_Read_Bytes((FRAM_NVM_ADDR+data_num*5U), read_data, 5);
				OS_EXIT_CRITICAL();

				read_data[4] = read_data[0] + read_data[1] + read_data[2] + read_data[3];

				if(save_data[4]==read_data[4])
				{
					ret_sts = 1;//succ
					break;
				}
				else
				{
					vTaskDelay(6);
				}
			}
			else
			{
				ret_sts = 2;//fsh
				data_num = 0;
			}
		}

		if(res == 3 && ret_sts==0)
		{
			ret_sts = 3;//fail
		}

		data_num ++;

	}
	else
	{
		readrtcflg = 0;
		ret_sts = 0;
		data_num = 0;
	}

	return ret_sts;
}



void EEPROM_vReadBytes(uint16_T datalen, uint32_T *readdata)
{

	 uint8_T  i;
	 uint8_T read_data[5];
	 uint8_T chk;

	 for(i=0;i<datalen;i++)
	 {
	     Delay_ms(4U);

	     OS_ENTER_CRITICAL();
	     exEEP_Read_Bytes((FRAM_NVM_ADDR+i*5U),read_data,5);
	     OS_EXIT_CRITICAL();

	     chk = read_data[0] + read_data[1] + read_data[2] + read_data[3];

	     if(chk == read_data[4])
	     {
	    	 readdata[i] = ((uint32_T)(read_data[0])<<24)+((uint32_T)(read_data[1])<<16)+((uint32_T)(read_data[2])<<8)+((uint32_T)(read_data[3])<<0);
	     }
	     else
	     {
	    	 readdata[i] = 0;
	     }


	 }
}



void EEPROM_ReadInitEEP(uint32_T *readdata)
{

	 uint8_T  i;
	 uint8_T read_data[5];
	 uint8_T chk;

	 for(i=0;i<11;i++)
	 {
	     Delay_ms(4U);

	     OS_ENTER_CRITICAL();
	     exEEP_Read_Bytes((FRAM_NVM_ADDR+(i+20)*5U),read_data,5);
	     OS_EXIT_CRITICAL();

	     chk = read_data[0] + read_data[1] + read_data[2] + read_data[3];

	     if(chk == read_data[4])
	     {
	    	 readdata[i] = ((uint32_T)(read_data[0])<<24)+((uint32_T)(read_data[1])<<16)+((uint32_T)(read_data[2])<<8)+((uint32_T)(read_data[3])<<0);
	     }
	     else
	     {
	    	 readdata[i] = 0;
	     }


	 }


}


void EEPROM_WriteInitEEP(uint32_T *writedata)
{
	uint8_T save_data[5];
	uint8_T read_data[5];

	static uint16_T data_num=0;
	uint8_T res = 0;

	if(vewm_enum_SleepSt==1)
	{

		for(res=0U;res<3U;res++)
		{
			if(data_num < 11)
			{
				save_data[0] = (uint8_T)(writedata[data_num]>>24);
				save_data[1] = (uint8_T)(writedata[data_num]>>16);
				save_data[2] = (uint8_T)(writedata[data_num]>>8);
				save_data[3] = (uint8_T)(writedata[data_num]>>0);
				save_data[4] = save_data[0] + save_data[1] + save_data[2] + save_data[3];

				OS_ENTER_CRITICAL();
				exEEP_Write_Bytes((FRAM_NVM_ADDR+(data_num+20)*5U), save_data, 5);
				OS_EXIT_CRITICAL();
				vTaskDelay(6);
				OS_ENTER_CRITICAL();
				exEEP_Read_Bytes((FRAM_NVM_ADDR+(data_num+20)*5U), read_data, 5);
				OS_EXIT_CRITICAL();

				read_data[4] = read_data[0] + read_data[1] + read_data[2] + read_data[3];

				if(save_data[4]==read_data[4])
				{
					break;
				}
				else
				{
					vTaskDelay(6);
				}
			}
			else
			{
				data_num = 0;
			}
		}

		data_num ++;

	}
	else
	{
		data_num = 0;
	}


}
